%% Author: Administrator
%% Created: 2009-3-21
%% Description: TODO: Add description to benchmark_app
-module(webserver_app).

-behaviour(application).
-behaviour(supervisor).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% Behavioural exports
%% --------------------------------------------------------------------
%% application behaviour callback
-export([
     start/2,
     stop/1
        ]).

%% supervisor behaviour callback
-export([init/1]).

-export([start/0]).

%% --------------------------------------------------------------------
%% Internal exports
%% --------------------------------------------------------------------
-export([]).

%% --------------------------------------------------------------------
%% Macros
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% Records
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% API Functions
%% --------------------------------------------------------------------


%% ====================================================================!
%% External functions
%% ====================================================================!
start() ->
    application:start(webserver).

%% --------------------------------------------------------------------
%% Func: start/2
%% Returns: {ok, Pid}        |
%%          {ok, Pid, State} |
%%          {error, Reason}
%% --------------------------------------------------------------------
start(_Type, _StartArgs) ->
    app_util:ensure_base_apps(),
%    app_util:init_logger(benchmark),
    app_util:init_default_logger("webserver"), % benchmark should not with ring_node or other apps
    NodeName = list_to_atom("webserver@" ++ app_util:get_host_addr_str()),
    net_kernel:start([NodeName, longnames]),

    RingLordNode = 
    case init:get_argument(ringlord) of
        {ok, [[RingLordStr]]} ->
            list_to_atom("ringlord@" ++ RingLordStr);
        {ok, [[]]} ->    
            'ringlord@10.0.0.253'
    end,
    true = net_kernel:connect_node(RingLordNode),
    global:sync(),
    
    supervisor:start_link({local, ?MODULE}, ?MODULE, [RingLordNode]).

%% --------------------------------------------------------------------
%% Func: stop/1
%% Returns: any
%% --------------------------------------------------------------------
stop(_State) ->
    ok.

init([RingLordNode]) ->
    log4erl:debug("webserver start..."),
    
    Ip = case os:getenv("MOCHIWEB_IP") of false -> "0.0.0.0"; Any -> Any end,   
    WebConfig = [
        {ringlord, RingLordNode},
        {ip, Ip},
        {port, 8000},
        {docroot, mochi_deps:local_path(["priv", "www"])}
    ],
    Web = {
        web_server,
        {web_server, start, [WebConfig]},
        permanent, 5000, worker, dynamic
    },
    RingAccessor = {
        ring_accessor,
        {ring_accessor, start_link, [RingLordNode]},
        permanent, brutal_kill, worker, [ring_accessor]
    },

    Processes = [Web, RingAccessor],
    {ok, {{one_for_one, 10, 10}, Processes}}.
    
    
%% ====================================================================
%% Internal functions
%% ====================================================================

